package cn.tom.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.tom.dao.UserMapper;
import cn.tom.entity.Usr;
import cn.tom.entity.WxQrInfo;
import cn.tom.entity.WxUserInfo;
import cn.tom.service.UserService;
import cn.tom.tool.JwtUtil;
import cn.tom.tool.RedisCache;
import cn.tom.tool.XJson;
import cn.tom.tool.XResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RequestMapping({"/api/public/wxLogin"})
@RestController
public class WxLoginController {
    private  final  String secret = "ac8260b7"; //易登回调唯一标识
    @Autowired
    UserService userService;
    @Autowired
    RedisCache redisCache;
    @RequestMapping("/callBack")
    public XJson wxCallBack(@RequestBody String body) {
        System.out.println("微信登陆---------------------");
        System.out.println(body);
        WxUserInfo wxUserInfo = JSONUtil.toBean(body, WxUserInfo.class);
        System.out.println("WxUserInfo=========" + wxUserInfo);

        Usr usr = new Usr();
        usr.setAccount(wxUserInfo.getWxMaUserInfo().getOpenId());
        usr.setNickname(wxUserInfo.getWxMaUserInfo().getNickName());
        usr.setRole("usr");
        // 创建 Token
        String token = JwtUtil.createToken(usr.getAccount(),usr.getRole());
        Map<String, String> map = new HashMap();
        map.put("token", token);
        map.put("role",usr.getRole());
        map.put("nickname",usr.getNickname());
        map.put("account",usr.getAccount());
        redisCache.setCacheObject(wxUserInfo.getTempUserId(),map,60, TimeUnit.SECONDS);
        Usr byAccount = userService.findByAccount(usr.getAccount());

        if (byAccount!=null) {
            return XResp.pack(0,"登录成功",null);
        }

        usr.setImg(wxUserInfo.getWxMaUserInfo().getAvatarUrl());
        System.out.println(usr.toString());
        userService.add(usr);
        System.out.println("uid===========" +usr.getId());
        System.out.println("=================完成======");
        return XResp.pack(0,"登录成功",usr);
    }

    @RequestMapping("/wxQr")
    @ResponseBody
    public XJson wxQr() {
        // 请求易登获取二维码接口
        String s = HttpUtil.get("https://yd.jylt.cc/api/wxLogin/tempUserId?secret=" + secret);
        JSONObject jsonObject = JSONUtil.parseObj(s);
        Integer code = jsonObject.getInt("code");
        if (jsonObject.getInt("code") != 0) {
            return XResp.pack(code, "二维码获取失败",null);
        }
        WxQrInfo wxQrInfo = jsonObject.get("data", WxQrInfo.class);
        return XResp.pack(0,"",wxQrInfo);
    }

    @RequestMapping("/wx/is_login")
    @ResponseBody
    public XJson wxIsLogin(String tempUserId, HttpServletResponse response) {
        if (redisCache.getCacheObject(tempUserId)==null) {
            return XResp.pack(201,"登录失败",null);
        }
        Map<String,String> map = redisCache.getCacheObject(tempUserId);
        return XResp.pack(200,"登录成功",map);
    }
}
